package lt.liepasoft.locr.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import lt.liepasoft.locr.data.SymbolImage;

public class SymbolDrawPanel extends JPanel{

    public final int panelWidth, panelHeight;
    private int cellWidth, cellHeight, offX, offY;

    private SymbolImage symbolImage;

    public SymbolDrawPanel(int panelWidth, int panelHeight, boolean mouseListener) {
        super(true);
        this.panelWidth = panelWidth;
        this.panelHeight = panelHeight;
        this.setPreferredSize(new Dimension(panelWidth, panelHeight));
        if (mouseListener){
            MouseAdapter myMouseAdapter = new MouseAdapter() {           
                private boolean erase = false;

                @Override
                public void mousePressed(MouseEvent e) {
                    //super.mousePressed(e);
                    int 
                        x = e.getX()-offX,
                        y = e.getY()-offY;
                    x /= cellWidth;
                    y /= cellHeight;
                    if (x >= 0 && y >= 0 && x < symbolImage.width && y < symbolImage.height){
                        if (symbolImage.getPixel(x, y)){
                            symbolImage.setPixel(false, x,y);
                            erase = false;
                        } else {
                            symbolImage.setPixel(true, x,y);
                            erase = true;
                        }
                        updateUI();
                    }                

                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    int 
                        x = e.getX()-offX,
                        y = e.getY()-offY;
                        x /= cellWidth;
                        y /= cellHeight;
                    if (x >= 0 && y >= 0 && x < symbolImage.width && y < symbolImage.height){
                        symbolImage.setPixel(erase, x,y);
                        updateUI();
                    }
                }                        
            };
            this.addMouseListener(myMouseAdapter);
            this.addMouseMotionListener(myMouseAdapter);
        }
    }

//    public SymbolDrawPanel(int panelWidth, int panelHeight, SymbolImage symbolImage) {
//        this(panelWidth, panelHeight);        
//        setSymbolImage(symbolImage); 
//    }    
    
    public final void setSymbolImage(SymbolImage symbolImage) {
        this.symbolImage = symbolImage;
        if (symbolImage != null){
            cellWidth = panelWidth/symbolImage.width;
            cellHeight = panelHeight/symbolImage.height;
            offX = (panelWidth-(cellWidth*symbolImage.width))/2;
            offY = (panelHeight-(cellHeight*symbolImage.height))/2;
            //System.out.println(cellWidth+" "+cellHeight+" "+offX+" "+offY);
        }
        updateUI();
    }

    public SymbolImage getSymbolImage() {
        return symbolImage;
    }
    
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.darkGray);
        g.fillRect(0, 0, panelWidth, panelHeight);
        if (symbolImage == null) return;                        

        g.setColor(Color.lightGray);
        g.fillRect(offX, offY, symbolImage.width*cellWidth, symbolImage.height*cellHeight);

        g.setColor(Color.BLACK);
        for (int x=0; x<symbolImage.width; x++){
            for (int y=0; y<symbolImage.height; y++){
                if (symbolImage.getPixel(x, y)){
                    g.setColor(Color.BLACK);
                } else {
                    g.setColor(Color.WHITE);
                }
                g.fillRect(offX+x*cellWidth, offY+y*cellHeight, cellWidth, cellHeight);
                g.setColor(Color.DARK_GRAY);
                g.drawRect(offX+x*cellWidth, offY+y*cellHeight, cellWidth, cellHeight); 
            }
        }
    }               
}
